package asteroids.model.expressions;

import java.util.*;
import asteroids.model.types.*;

import exceptions.IllegalOperandException;

/**
 * A subclass of expression (E) that represents the not (!) operation of a boolean
 * 
 * @version 1.0
 * 
 * @author	Thijs Peirelinck & Wouter Parys
 *
 */
public class Not extends UnaryExpression {

	public Not(E e) {
		super(e);
	}
	
	@Override
	public Boolean getValue() {
		if(!this.isValidExpression()) { throw new IllegalOperandException(); }
		return !((Boolean)getOperand().getValue());
	}
	
	@Override
	public boolean isValidExpression() {
		return (getOperand().getReturnType().equals("boolean") && !(getOperand() == this));
	}
	
	@Override
	public String getReturnType() {
		return "boolean";
	}
	
	@Override
	public String toString() {
		return "!(" + getOperand().toString() + ")";
	}

	@Override
	public E createCopy() {
		return new Not(getOperand().createCopy());
	}
	
	@Override
	public boolean typeCheck(Map<String, T> globals) {
		return super.typeCheck(globals, "boolean");
	}
}
